import SysUtil from './utils/SysUtil'
import { ipcRenderer } from 'electron'
import { defaultHt } from './common'

const diskCalc = 1000 * 1000 * 1000

/**
 * 磁盘信息
 * @returns {Promise<void>}
 */
const allDisks = async () => {
  const diskSize = await SysUtil.diskSize()

  let disks = {}
  if (process.platform === 'win32') {
    disks = await calcDisk4Win(diskSize)
  } else if (process.platform === 'darwin') {
    const diskInfo = await SysUtil.diskInfo()
    disks = calcDisk4Mac(diskInfo, diskSize)
  }

  window.topInfo[1] = disks.diskUsage

  return new Promise((resolve) => {
    resolve(disks)
  })
}

/**
 * 计算windows系统磁盘信息
 * @param diskSize
 * @param diskDetailSize
 */
const calcDisk4Win = async (diskSize) => {
  const disks = {
    disks: [],
    diskUsage: 0
  }
  let totalUsed = 0,
    totalSize = 0
  const diskDetailSize = await SysUtil.diskDetailSize()
  diskSize.forEach((disk) => {
    const mount = disk.mount
    try {
      diskDetailSize.forEach((dt) => {
        if (dt.device !== '' && mount === dt.mount) {
          disk.label = dt.label
          throw new Error()
        }
      })
    } catch {
      /* empty */
    }
    totalSize += disk.size
    totalUsed += disk.used
    disk.available = (disk.available / diskCalc).toFixed(2)
    disk.size = (disk.size / diskCalc).toFixed(0)
    disk.used = (disk.used / diskCalc).toFixed(2)
    disks.disks.push(disk)
  })

  // 计算所有磁盘使用率
  const rate = Number(((totalUsed / totalSize) * 100).toFixed(2))
  disks.diskUsage = rate !== 100 ? rate : 100

  // console.log(disks)

  return new Promise((resolve) => {
    resolve(disks)
  })
}

/**
 * 计算mac磁盘信息
 * @param diskInfo
 * @param diskSize
 */
const calcDisk4Mac = (diskInfo, diskSize) => {
  const disks = {
    disks: [],
    diskUsage: 0
  }
  let totalSize = 0,
    totalUsed = 0
  diskInfo.forEach((disk) => {
    const dk = {
      device: disk.device, //  === 'disk0' ? 'disk1' : disk.device
      name: disk.name,
      interfaceType: disk.interfaceType,
      size: (disk.size / diskCalc).toFixed(0),
      children: [],
      available: 0,
      use: 0,
      used: 0
    }

    let partitions = []
    let available = 0
    let used = 0
    diskSize.forEach((item) => {
      const mount = item.mount
      const fs = item.fs

      let child = {
        name: mount.substring(mount.lastIndexOf('/') + 1),
        available: (Number(item.available) / diskCalc).toFixed(2),
        size: (Number(item.size) / diskCalc).toFixed(0),
        use: item.use,
        used: (Number(item.used) / diskCalc).toFixed(2)
      }

      let hasChild = false
      if (dk.device === 'disk0' && (mount === '/' || mount.startsWith('/System/Volumes'))) {
        partitions.push(child)
        hasChild = true
      } else {
        const dev = fs.substring(fs.lastIndexOf('/') + 1).substring(0, dk.device.length)
        if (dk.device === dev) {
          // console.log(`name = ${dk.device},dev = ${dev}`)
          partitions.push(child)
          hasChild = true
        }
      }

      if (hasChild) {
        available = Number(child.available)
        used += Number(child.used)
      }
    })

    if (available > 0 || used > 0) {
      dk.available = available
      dk.use = ((used / Number(dk.size)) * 100).toFixed(2)
      dk.used = used.toFixed(2)
      dk.children = partitions
      disks.disks.push(dk)

      totalSize += Number(dk.size)
      totalUsed += Number(dk.used)
    }
  })
  // 计算所有磁盘使用率
  const rate = Number(((totalUsed / totalSize) * 100).toFixed(2))
  disks.diskUsage = rate !== 100 ? rate : 100

  return disks
}

/**
 * 计算mac磁盘信息
 * @param diskInfo
 * @param diskSize
 */
const getDisksUsage = (diskInfo) => {
  const disks = {
    disks: [],
    diskUsage: 0
  }
  let totalSize = 0,
    totalUsed = 0
  diskInfo.forEach((disk) => {
    const dk = {
      device: disk.device, //  === 'disk0' ? 'disk1' : disk.device
      name: disk.name,
      interfaceType: disk.interfaceType,
      size: (disk.size / diskCalc).toFixed(0),
      children: [],
      available: 0,
      use: 0,
      used: 0
    }

    let partitions = []
    diskSize.forEach((item) => {
      const mount = item.mount
      const fs = item.fs

      let child = {
        name: mount.substring(mount.lastIndexOf('/') + 1),
        available: (Number(item.available) / diskCalc).toFixed(2),
        size: (Number(item.size) / diskCalc).toFixed(0),
        use: item.use,
        used: (Number(item.used) / diskCalc).toFixed(2)
      }

      if (dk.device === 'disk0' && (mount === '/' || mount.startsWith('/System/Volumes'))) {
        partitions.push(child)
      } else {
        const dev = fs.substring(fs.lastIndexOf('/') + 1).substring(0, dk.device.length)
        if (dk.device === dev) {
          // console.log(`name = ${dk.device},dev = ${dev}`)
          partitions.push(child)
        }
      }
    })

    let available = 0
    let used = 0
    if (partitions.length > 0) {
      partitions.forEach((item) => {
        available = Number(item.available)
        used += Number(item.used)
      })
    }

    if (available > 0 || used > 0) {
      dk.available = available
      dk.use = ((used / Number(dk.size)) * 100).toFixed(2)
      dk.used = used.toFixed(2)
      dk.children = partitions
      disks.disks.push(dk)

      totalSize += Number(dk.size)
      totalUsed += Number(dk.used)
    }
  })
  // 计算所有磁盘使用率
  const rate = Number(((totalUsed / totalSize) * 100).toFixed(2))
  disks.diskUsage = rate !== 100 ? rate : 100

  return disks
}

/**
 * 设置窗口高度
 * @param props
 */
const setDiskHt = (props) => {
  if (props.dSizeDetail && props.dSizeDetail.length > 0) {
    const ht = props.dSizeDetail.length * 80
    ipcRenderer.send('winHtChange', ht + defaultHt)
  }
}

export { allDisks, setDiskHt }
